////////////////////////////////////////////////
// -- DepthHUD Lite                           //
// by Hurricaaane (Ha3)                       //
//                                            //
// http://www.youtube.com/user/Hurricaaane    //
//--------------------------------------------//
// Canvas                                     //
////////////////////////////////////////////////
local dhlite = dhlite


function dhlite:MountCanvas()
	self:MakeCanvas()
	
	self._elts = {}
	self._skin_name = "DHLITE_Skin" .. DHLITE_DEBUG_SKINGENERATOR
	self._bEditMode = false
	self._bMoveMode = false
	self._vMoveModePanel = nil
	self._iMoveX = 0
	self._iMoveY = 0
	
end

function dhlite:GetCanvas()
	return self._canvas
	
end

function dhlite:__DebugMakeElements()
	if not self.__debug_made_elements and (self:GetVar("core_debug") > 0) then
		local w, h = 80, 32
		
		local elt = vgui.Create( "DHUI_Health", self._canvas )
		elt:SetCanvas( self._canvas )
		elt:SetSize( w, h )
		table.insert( self._elts, elt )
		
		local elt = vgui.Create( "DHUI_Speedometer", self._canvas )
		elt:SetCanvas( self._canvas )
		elt:SetSize( w, h )
		table.insert( self._elts, elt )
		
		
		local elt = vgui.Create( "DHUI_RealTimeVariator", self._canvas )
		elt:SetCanvas( self._canvas )
		elt:SetSize( 1.5*w, h*0.9 )
		table.insert( self._elts, elt )
		
		local elt = vgui.Create( "DHUI_Armor", self._canvas )
		elt:SetCanvas( self._canvas )
		elt:SetSize( w, h )
		table.insert( self._elts, elt )
		
		local elt = vgui.Create( "DHUI_Time", self._canvas )
		elt:SetCanvas( self._canvas )
		elt:SetSize( 1.5*w, h*0.9 )
		table.insert( self._elts, elt )
		
		for k,obj in pairs( self._elts ) do
			obj:Show( )
			obj:SetMouseInputEnabled( true )
			obj:SetKeyboardInputEnabled( false )
			obj:SetSkin( self._skin_name )
			obj:SetVirtualGridPos( 16, 0, k - 1 )
			obj:ApplyVirtualGridPos( )
			
		end
		
		self.__debug_made_elements = true
	
	elseif self.__debug_made_elements and (self:GetVar("core_debug") <= 0) then
		for k,elt in pairs( self._elts ) do
			elt:Remove()
			
		end
		self._elts = {}
		
		self.__debug_made_elements = false
		
	end
	
end

function dhlite:UnmountCanvas( )
	self:RemoveCanvas( )
	
end

function dhlite:MakeCanvas( )
	if not ValidPanel( self._canvas ) then
		//debug padding
		local debug_padding = 8
		self._canvas = vgui.Create( "DPanel" )
		self._canvas:SetSize( ScrW() - debug_padding * 2, ScrH() - debug_padding * 2 )
		self._canvas:SetPaintBackground( false )
		self._canvas:SetPos( debug_padding, debug_padding )
		self:SetCanvasEditable( false )
		
	end
	
end

function dhlite:SetCanvasEditable( bSet )
	if not ValidPanel( self._canvas ) then return end
	
	self._bEditMode = bSet
	self._canvas:SetMouseInputEnabled( bSet )
	self._canvas:SetKeyboardInputEnabled( bSet )
	
	if bSet then
		self._canvas:SetParent( nil ) // !!
	
	else
		self._canvas:ParentToHUD() // !!
		
	end
	
end

function dhlite.HUDPaint( )
	if not dhlite._bEditMode then return end
	local self = dhlite
	
	local mx, my = gui.MousePos( )
	surface.SetTexture( surface.GetTextureID( "dhlite/beacon_square_4" ) )
	
	surface.SetDrawColor( 255, 255, 255, 255 )
	surface.DrawTexturedRectRotated( mx, my, 16, 16, 0)
	if self._bMoveMode then
		surface.SetTexture( surface.GetTextureID( "dhlite/beacon_flare_add" ) )
		surface.DrawTexturedRectRotated( mx, my, 64, 64, CurTime() * 45)
		
	end
	
end

function dhlite.Think( )
	if not dhlite._bEditMode then return end
	local self = dhlite
	
	if self._bMoveMode then
		local bDoMove = input.IsMouseDown( MOUSE_LEFT )
		
		if bDoMove then
			local snap, xgrid, ygrid = self._vMoveModePanel:EvaluateVirtualGridPos( self:GetVar( "editor_snap" ), self._iMoveX, self._iMoveY  )
			
			/*
			local canvas = self._vMoveModePanel:GetCanvas()
			
			local mx, my = canvas:CursorPos()
			local px, py = mx - self._iMoveX, my - self._iMoveY
			
			local gw, gh = self._vMoveModePanel:GetWide(), self._vMoveModePanel:GetTall()
			
			--local cx, cy = self._canvas:GetPos()
			//local cx, cy = 0, 0
			local cw, ch = canvas:GetWide(), canvas:GetTall()
			//local cxm, cym = cx + cw, cy + ch
			
			local snap = self:GetVar( "editor_snap" )
			local cws, chs = (cw - gw) / snap, (ch - gh) / snap
			local xmar, ymar = px % cws, py % chs
			
			local xgrid = (px - xmar) / cws + (((xmar / cws) < 0.5) and 0 or 1)
			local ygrid = (py - ymar) / chs + (((ymar / chs) < 0.5) and 0 or 1)*/
			
			local prS, prX, prY = self._vMoveModePanel:GetVirtualGridPos( )
			if not (prS == snap and prX == xgrid and prY == ygrid) then
				self._vMoveModePanel:SetVirtualGridPos( snap, xgrid, ygrid )
				self._vMoveModePanel:ApplyVirtualGridPos( 0.3, 0.3 )
			end
			
		else
			self:FinishMoveMode( )
			
		end
		
	end
	
end

function dhlite:FinishMoveMode()
	if not self._bMoveMode then return end
	
	self._bMoveMode = false
	surface.PlaySound( "dhlite/load_off.wav" )
	local res, x, y = self._vMoveModePanel:GetVirtualGridPos()
	chat.AddText( Color(255, 0, 0), "(" .. res .. ") : ".. x .. ", " .. y .. ";" )
	
end

function dhlite:IsEditMode()
	return self._bEditMode
end

function dhlite:IsMoveMode()
	return self._bMoveMode
end

function dhlite:GetMoveModePanel()
	return self._vMoveModePanel
	
end

function dhlite:RequestMouseInput( panel, mousecode )
	if self._bMoveMode then return end
	if mousecode ~= MOUSE_LEFT then return end
	
	self._iMoveX, self._iMoveY = panel:CursorPos()
	
	surface.PlaySound( "dhlite/load_on.wav" )
	
	self._bMoveMode = true
	self._vMoveModePanel = panel
	
end

function dhlite:RemoveCanvas( )
	if ValidPanel( self._canvas ) then
		self._canvas:Remove( )
		self._canvas = nil
		
	end
	
end

function dhlite:HideCanvas( )
	if ValidPanel( self._canvas ) and self._canvas:IsVisible( ) then
		self._canvas:SetVisible( false )
		
	end
	
end

function dhlite:ShowCanvas( )
	if ValidPanel( self._canvas ) and not self._canvas:IsVisible( ) then
		self._canvas:SetVisible( true )
		
	end
	
end